<?php

class indexController extends Controller {
	
	public $initphp_list = array(
		'index',
		'fields', 'postfields',
		'tables', 'posttables',
		'etables',	'postetables',
		'operations', 'postoperations',
		'buildadd', 'postbuildadd',
		'sqls', 'postsqls',
		'dbs'
	);
	
	private $_service;
	
	public function before()
	{
		$this->_service = InitPHP::getService('itools');
		
		$this->view->set_tpl('head', 'F');
		
		$menu = array(
			array('name'=>'首页', 'url'=>InitPHP::url('index|index')),
			array('name'=>'字段库', 'url'=>InitPHP::url('index|fields')),
			array('name'=>'创建业务库', 'url'=>InitPHP::url('index|tables')),
			array('name'=>'修改业务库', 'url'=>InitPHP::url('index|etables')),
			array('name'=>'生成业务库', 'url'=>InitPHP::url('index|sqls')),
			array('name'=>'操作库', 'url'=>InitPHP::url('index|operations')),
			array('name'=>'构建app目录', 'url'=>InitPHP::url('index|buildadd')),
			array('name'=>'数据库概况', 'url'=>InitPHP::url('index|dbs')),
		);
		$this->view->assign('topmenu', $menu);
		$this->view->set_tpl('menu');
	}
	public function run()
	{
	}
	public function after()
	{
		$this->view->set_tpl('foot', 'L');
		$this->view->display();
	}
	
	public function index()
	{
		$this->view->set_tpl('main');
	}
	
	//字段库
	public function fields()
	{
		$fields = $this->_service->get_field_types();
		$this->view->assign('fields', $fields);
		
		$colors = array();
		
		$bases = $this->_service->get_field_base();
		foreach ($bases as $k => $v)
		{
			$sort = ceil($v['field_sort']/10);
			
			if (!isset($colors[$sort]))
			{
				$colors[$sort] = $this->makecolor();
			}
			$v['bgcolor'] = $colors[$sort];
			
			$bases[$k] = $v;
		}
		$this->view->assign('bases', $bases);
		
		$actionurl = InitPHP::url('index|postfields');
		$this->view->assign('actionurl', $actionurl);
		
		$this->view->set_tpl('fields');
	}
	//字段库提交
	public function postfields()
	{
		$data = $this->controller->get_post();
		if (empty($data['new']) && empty($data['old']))	$this->controller->redirect(InitPHP::url('index|fields'));
		
		if (!empty($data['new']))
		{
			foreach ($data['new'] as $v)
			{
				$this->_service->insert_field_base($v);
			}
		}
		
		if (!empty($data['old']))
		{
			foreach ($data['old'] as $k => $v)
			{
				if (empty($v['field_name']))
				{
					$this->_service->delete_field_base($k);
					continue;
				}
				
				$this->_service->update_field_base($v, $k);
			}
		}
		
		$this->controller->redirect(InitPHP::url('index|fields'));
	}
	
	//构建业务库
	public function tables()
	{
		$fields = $this->_service->get_field_types();
		$this->view->assign('fields', $fields);
		
		$bases = $this->_service->get_field_base();
		$this->view->assign('bases', $bases);
		
		$exts = array('base', 'extend', 'spec', 'status');
		$this->view->assign('exts', $exts);
		
		$actionurl = InitPHP::url('index|posttables');
		$this->view->assign('actionurl', $actionurl);
		
		$this->view->set_tpl('tables');
	}
	public function posttables()
	{
		$data = $this->controller->get_post();
		if (empty($data['table']) || empty($data['fields']))	$this->controller->redirect(InitPHP::url('index|tables'));
		
		if (! empty($data['key']))
		{
			$keyfield = $data['fields'][$data['key']];
			unset($data['fields'][$data['key']]);
			
			unset($keyfield['belong']);
			$keyfield['is_key'] = 1;
		}

		$tables = array();
		foreach ($data['table']['ext'] as $v)
		{
			$name = $data['table']['name'].'_'.$v;
			$tables[$name] = array($keyfield);
		}
		foreach ($data['fields'] as $v)
		{
			$name = $data['table']['name'].'_'.$v['belong'];
			unset($v['belong']);
			
			$tables[$name][] = $v;
		}
		
		foreach ($tables as $k => $v)
		{
			foreach ($v as $n)
			{
				$n['table_name'] = $k;
				
				$this->_service->insert_table_base($n);
			}
		}
		
		$this->controller->redirect(InitPHP::url('index|tables'));
	}
	
	//修改业务库
	public function etables()
	{
		
	}
	public function postetables()
	{
		
	}
	
	//生成业务库
	public function sqls()
	{
		
	}
	public function postsqls()
	{
		
	}
	
	//操作库
	public function operations()
	{
		$bases = $this->_service->get_operation_base();
		$this->view->assign('bases', $bases);
		
		$actionurl = InitPHP::url('index|postoperations');
		$this->view->assign('actionurl', $actionurl);
		
		$this->view->set_tpl('operations');
	}
	//操作库提交
	public function postoperations()
	{
		$data = $this->controller->get_post();
		if (empty($data['new']) && empty($data['old']))	$this->controller->redirect(InitPHP::url('index|operations'));
		
		if (!empty($data['new']))
		{
			foreach ($data['new'] as $v)
			{
				$this->_service->insert_operation_base($v);
			}
		}
		
		if (!empty($data['old']))
		{
			foreach ($data['old'] as $k => $v)
			{
				if (empty($v['field_name']))
				{
					$this->_service->delete_operation_base($k);
					continue;
				}
				
				$this->_service->update_operation_base($v, $k);
			}
		}
		
		$this->controller->redirect(InitPHP::url('index|operations'));
	}
	
	public function dbs()
	{
		
	}
	
	//构建app
	public function buildadd()
	{
		$actionurl = InitPHP::url('index|postbuildadd');
		$this->view->assign('actionurl', $actionurl);
		
		$this->view->set_tpl('buildapp');
	}
	//构建app提交
	public function postbuildadd()
	{
		$data = $this->controller->get_post();
		if (empty($data['app']))	$this->controller->redirect(InitPHP::url('index|buildadd'));
		
		$this->make_app_dirs($data['app']['modulename']);
		
		//$this->controller->redirect(InitPHP::url('index|index'));
	}

	//自动创建app的目录结构
	private function make_app_dirs($app = '')
	{
		$root = dirname(dirname(str_replace('\\', '/', realpath(APP_PATH))));
		$root = rtrim($root, '/').'/';
		
		$res = 0;
		$dirs = array(
			'',
			'www',
			'www/data',
			'www/static',
			'app',
			'app/conf',
			'app/hook',
			'app/data',
			'app/data/template_c',
			'app/data/filecache',
			'app/data/html',
			'app/library',
			'app/library/dao',
			'app/library/helper',
			'app/library/service',
			'app/library/test',
			'app/web'
		);
		foreach ($dirs as $dir)
		{
			$res += @mkdir($root.$app.'/'.$dir);
		}
		
		return $res == count($dirs);
	}
	//自动创建，包括：默认index，默认配置等
	private function make_app_files($app = '')
	{
		$root = dirname(dirname(str_replace('\\', '/', realpath(APP_PATH))));
		$root = rtrim($root, '/').'/';
		
		@copy(rtrim(dirname($root), '/').'/initphp/initphp.conf.php', $root.$app.'/app/conf/comm.conf.php');
	}
	
	private function makecolor()
	{
		$color = '#';
		
		for ($i=1; $i<=3 ;$i++)
		{
			$color .= dechex(rand(128, 255));
		}
		
		return $color;
	}

 } 